load("@rules_rust//rust:defs.bzl", "rust_library")
load("//rs/tests:common.bzl", "COUNTER_CANISTER_RUNTIME_DEPS", "GRAFANA_RUNTIME_DEPS", "IMPERSONATE_UPSTREAMS_ENV", "IMPERSONATE_UPSTREAMS_RUNTIME_DEPS", "MESSAGE_CANISTER_ENV", "MESSAGE_CANISTER_RUNTIME_DEPS", "UNIVERSAL_CANISTER_ENV", "UNIVERSAL_CANISTER_RUNTIME_DEPS")
load("//rs/tests:system_tests.bzl", "system_test", "system_test_nns")

package(default_visibility = ["//rs:system-tests-pkg"])

rust_library(
    name = "node_registration_test_common",
    testonly = True,
    srcs = ["node_registration_test_common/src/lib.rs"],
    crate_name = "ic_consensus_system_test_node_registration_test_common",
    deps = [
        # Keep sorted.
        "//rs/nns/constants",
        "//rs/nns/governance/api",
        "//rs/protobuf",
        "//rs/registry/canister",
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/base_types",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:slog",
    ],
)

rust_library(
    name = "catch_up_test_common",
    testonly = True,
    srcs = ["catch_up_test_common/src/lib.rs"],
    crate_name = "ic_consensus_system_test_catch_up_test_common",
    deps = [
        # Keep sorted.
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:futures",
        "@crate_index//:slog",
    ],
)

system_test(
    name = "catch_up_loop_prevention_test",
    # TODO(NET-1683): Adjust test for faster p2p
    tags = [
        "long_test",
    ],
    test_timeout = "eternal",
    uses_guestos_img = False,
    uses_guestos_malicious_img = True,
    runtime_deps = GRAFANA_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        ":catch_up_test_common",
        "//rs/registry/subnet_type",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/types",
        "@crate_index//:anyhow",
    ],
)

system_test(
    name = "catch_up_possible_test",
    # TODO(NET-1683): Adjust test for faster p2p
    tags = [
        "long_test",
    ],
    test_timeout = "eternal",
    uses_guestos_img = False,
    uses_guestos_malicious_img = True,
    runtime_deps = GRAFANA_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        ":catch_up_test_common",
        "//rs/registry/subnet_type",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/types",
        "@crate_index//:anyhow",
    ],
)

system_test(
    name = "dual_workload_test",
    env = UNIVERSAL_CANISTER_ENV,
    tags = [
        "colocate",
        "long_test",
    ],
    runtime_deps = UNIVERSAL_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/registry/subnet_type",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/base_types",
        "//rs/universal_canister/lib",
        "@crate_index//:anyhow",
        "@crate_index//:futures",
        "@crate_index//:ic-agent",
        "@crate_index//:slog",
        "@crate_index//:tokio",
    ],
)

rust_library(
    name = "liveness_test_common",
    testonly = True,
    srcs = ["liveness_test_common/src/lib.rs"],
    crate_name = "ic_consensus_system_test_liveness_test_common",
    deps = [
        # Keep sorted.
        "//rs/registry/subnet_type",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/base_types",
        "@crate_index//:ic-agent",
        "@crate_index//:rand",
        "@crate_index//:rand_chacha",
        "@crate_index//:slog",
        "@crate_index//:tokio",
    ],
)

system_test(
    name = "liveness_with_equivocation_test",
    env = UNIVERSAL_CANISTER_ENV,
    uses_guestos_img = False,
    uses_guestos_malicious_img = True,
    uses_guestos_test_update = True,
    runtime_deps = UNIVERSAL_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        ":liveness_test_common",
        "//rs/registry/subnet_type",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/types",
        "@crate_index//:anyhow",
    ],
)

system_test_nns(
    name = "max_ingress_payload_size_test",
    env = UNIVERSAL_CANISTER_ENV,
    runtime_deps = UNIVERSAL_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/limits",
        "//rs/registry/subnet_type",
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/base_types",
        "//rs/types/types",
        "//rs/universal_canister/lib",
        "@crate_index//:anyhow",
        "@crate_index//:assert_matches",
        "@crate_index//:futures",
        "@crate_index//:ic-agent",
        "@crate_index//:slog",
        "@crate_index//:tokio",
    ],
)

system_test(
    name = "max_xnet_payload_size_test",
    env = UNIVERSAL_CANISTER_ENV,
    runtime_deps = UNIVERSAL_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/registry/subnet_type",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/base_types",
        "//rs/universal_canister/lib",
        "@crate_index//:anyhow",
        "@crate_index//:futures",
        "@crate_index//:ic-agent",
        "@crate_index//:slog",
        "@crate_index//:tokio",
    ],
)

system_test_nns(
    name = "node_graceful_leaving_test",
    env = UNIVERSAL_CANISTER_ENV,
    runtime_deps = UNIVERSAL_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/registry/subnet_type",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/base_types",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:rand",
        "@crate_index//:rand_chacha",
        "@crate_index//:slog",
    ],
)

system_test_nns(
    name = "cup_explorer_test",
    tags = [
        "long_test",  # The P90 duration was over 6 minutes for the week starting on 2025-08-25.
    ],
    deps = [
        # Keep sorted.
        "//rs/cup_explorer",
        "//rs/nns/constants",
        "//rs/protobuf",
        "//rs/registry/canister",
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/tests/consensus/tecdsa/utils",
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/base_types",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:prost",
        "@crate_index//:rand",
        "@crate_index//:rand_chacha",
        "@crate_index//:slog",
        "@crate_index//:tempfile",
        "@crate_index//:tokio",
    ],
)

system_test_nns(
    name = "subnet_splitting_test",
    env = MESSAGE_CANISTER_ENV,
    tags = [
        "colocate",
        "long_test",  # since it takes longer than 5 minutes.
        "subnet_splitting",
    ],
    runtime_deps =
        MESSAGE_CANISTER_RUNTIME_DEPS + [
            "//rs/tests:recovery/binaries",
        ],
    deps = [
        "//rs/recovery",
        "//rs/recovery/subnet_splitting",
        "//rs/registry/routing_table",
        "//rs/registry/subnet_type",
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/base_types",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:candid",
        "@crate_index//:slog",
    ],
)

system_test(
    name = "guestos_recovery_engine_smoke_test",
    env = IMPERSONATE_UPSTREAMS_ENV | {
        "RECOVERY_ARTIFACTS_PATH": "$(rootpath //ic-os/guestos/envs/recovery-dev:recovery.tar.zst)",
        "RECOVERY_HASH_PATH": "$(rootpath //ic-os/guestos/envs/recovery-dev:recovery.tar.zst.sha256)",
        "RECOVERY_CUP_B64_PATH": "$(rootpath //ic-os/guestos/envs/recovery-dev:cup.proto.b64)",
        "RECOVERY_STORE_1_B64_PATH": "$(rootpath //ic-os/guestos/envs/recovery-dev:ic_registry_local_store_1.b64)",
        "RECOVERY_STORE_2_B64_PATH": "$(rootpath //ic-os/guestos/envs/recovery-dev:ic_registry_local_store_2.b64)",
    },
    tags = [
        "long_test",  # the P90 duration of this test is over 6 minutes in the week starting on 2025-08-25.
    ],
    uses_guestos_img = False,
    uses_guestos_recovery_dev_img = True,
    runtime_deps = IMPERSONATE_UPSTREAMS_RUNTIME_DEPS + [
        "//ic-os/guestos/envs/recovery-dev:cup.proto.b64",
        "//ic-os/guestos/envs/recovery-dev:ic_registry_local_store_1.b64",
        "//ic-os/guestos/envs/recovery-dev:ic_registry_local_store_2.b64",
        "//ic-os/guestos/envs/recovery-dev:recovery.tar.zst",
        "//ic-os/guestos/envs/recovery-dev:recovery.tar.zst.sha256",
    ],
    deps = [
        # Keep sorted.
        "//rs/registry/subnet_type",
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "@crate_index//:anyhow",
        "@crate_index//:slog",
        "@crate_index//:ssh2",
    ],
)

system_test(
    name = "replica_determinism_test",
    env = UNIVERSAL_CANISTER_ENV,
    uses_guestos_img = False,
    uses_guestos_malicious_img = True,
    uses_guestos_test_update = True,
    runtime_deps = UNIVERSAL_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/registry/subnet_type",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/types",
        "//rs/universal_canister/lib",
        "@crate_index//:anyhow",
        "@crate_index//:slog",
        "@crate_index//:tokio",
    ],
)

system_test(
    name = "request_auth_malicious_replica_test",
    env = UNIVERSAL_CANISTER_ENV,
    uses_guestos_img = False,
    uses_guestos_malicious_img = True,
    uses_guestos_test_update = True,
    runtime_deps = UNIVERSAL_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/crypto/test_utils/reproducible_rng",
        "//rs/registry/subnet_type",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/types",
        "//rs/universal_canister/lib",
        "@crate_index//:anyhow",
        "@crate_index//:ic-agent",
        "@crate_index//:k256",
        "@crate_index//:rand",
        "@crate_index//:reqwest",
        "@crate_index//:serde_cbor",
        "@crate_index//:slog",
        "@crate_index//:tokio",
        "@crate_index//:url",
    ],
)

system_test(
    name = "safety_test",
    env = UNIVERSAL_CANISTER_ENV,
    uses_guestos_img = False,
    uses_guestos_malicious_img = True,
    uses_guestos_test_update = True,
    runtime_deps = UNIVERSAL_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        ":liveness_test_common",
        "//rs/registry/subnet_type",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/types",
        "@crate_index//:anyhow",
    ],
)

system_test_nns(
    name = "consensus_performance",
    additional_colocate_tags = [
        "system_test_benchmark",
    ],
    colocated_test_driver_vm_required_host_features = ["performance"],
    colocated_test_driver_vm_resources = {
        "vcpus": 64,
        "memory_kibibytes": 512142680,
        "boot_image_minimal_size_gibibytes": 500,
    },
    crate_features = select({
        "//bazel:upload_perf_systest_results_enabled": ["upload_perf_systest_results"],
        "//conditions:default": [],
    }),
    enable_head_nns_variant = False,
    env = {"ENV_DEPS__IC_VERSION_FILE": "$(rootpath //bazel:version.txt)"},
    tags = [
        "colocate",
        "manual",
    ],
    test_timeout = "eternal",
    runtime_deps = GRAFANA_RUNTIME_DEPS + COUNTER_CANISTER_RUNTIME_DEPS + [
        "//bazel:version.txt",
        "//rs/tests:jaeger_uvm_config_image",
    ],
    deps = [
        # Keep sorted.
        "//rs/registry/subnet_type",
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:reqwest",
        "@crate_index//:serde_json",
        "@crate_index//:slog",
        "@crate_index//:tokio",
    ],
)

system_test_nns(
    name = "adding_nodes_to_subnet_test",
    env = MESSAGE_CANISTER_ENV,
    tags = [
        "long_test",  # since it takes longer than 5 minutes.
    ],
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/nns/constants",
        "//rs/nns/governance/api",
        "//rs/recovery",
        "//rs/registry/canister",
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/base_types",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:candid",
        "@crate_index//:slog",
    ],
)
